#! /usr/bin/env perl

###########################################################################
#
# vdel
#
###########################################################################
#
# This command removes files from VC (technically, it removes them from the HEAD of either trunk or branch,
# whichever the working copy is pointing at).  It also commits the removal.
#
# #########################################################################
#
# All the code herein is released under the Artistic License
#		( http://www.perl.com/language/misc/Artistic.html )
# Copyright (c) 2001-2008 Barefoot Software, Copyright (c) 2004-2007 ThinkGeek
#
###########################################################################

use strict;
use warnings;

use DirHandle;
use Cwd qw<getcwd>;

use VCtools::Base;
use VCtools::Args;
use VCtools::Common;


#################################
# OPTIONS AND ENVIRONMENT VARS
#################################

VCtools::switch('recursive', 'r', 'operate recursively');
VCtools::switch('force', 'f', "don't ask any questions");
VCtools::args('files', 'list', 'file(s) (or directory/ies) to remove from VC');
VCtools::getopts();

# remember, directories are files too
my @files = VCtools::files();


#################################
# CHECK FOR ERRORS
#################################

VCtools::verify_files_and_group(@files);

# get statuses for everything at once (quicker that way)
my @all_files = VCtools::cache_file_status(@files, { DONT_RECURSE => !VCtools::recursive() });

foreach my $file (@files)
{
	VCtools::fatal_error("$file is not in VC") unless VCtools::exists_in_vc($file);
}

# check for modified files if we don't have force specified
my @mod_files = VCtools::get_all_with_status('modified');
my $force_delete = 0;
if (@mod_files)
{
	unless (VCtools::force())
	{
		VCtools::list_files("are modified", @mod_files);
		VCtools::prompt_to_continue("Your modifications will be lost forever!");
	}
	$force_delete = 1;													# so VC delete command doesn't freak out
}


#################################
# MAIN
#################################

unless (VCtools::force())
{
	VCtools::list_files("will be removed", @all_files);
	VCtools::prompt_to_continue("files will be removed from VC's HEAD and then deleted from the working copy");
}

VCtools::remove_files(@all_files, { FORCE => $force_delete });
VCtools::commit_files(undef, @all_files, { DEL => scalar(@all_files) });# HACK! when first arg removed from lib, remove here

print "done\n";
